package com.micro.db.jdbc;

import com.micro.db.dialect.IJdbcTemplate;
import com.micro.disk.user.bean.Page;
import com.micro.disk.user.bean.TreeJson;
import com.micro.disk.user.bean.UserBean;
import com.micro.model.DiskUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class DiskOrganizationJdbc {
	@Autowired
	private IJdbcTemplate jdbcTemplate;

	public List<TreeJson> findTreeJsonList(String type){
		List<TreeJson> Page= new ArrayList<TreeJson>();
		try{
			String sql="select du.* from disk_organization du where du.disabled = 0";
			if(!"root".equalsIgnoreCase(type)){
				sql += " and du.type = ?";
			}
			Object[] args={
					type
			};
			List<Map<String,Object>> lists=jdbcTemplate.findList(sql,args);
			List<TreeJson> rows=new ArrayList<TreeJson>();

			if(!CollectionUtils.isEmpty(lists)){
				lists.forEach(m->{
					TreeJson row=new TreeJson();
					row.setId(m.get("id")==null?"":m.get("id").toString());
					row.setLabel(m.get("label")==null?"":m.get("label").toString());
					row.setType(m.get("type")==null?"":m.get("type").toString());
					row.setDisabled(false);
					rows.add(row);
				});
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return Page;
	}

	public List<TreeJson> findChildrenTreeJsonList(String pid,String type){
		List<TreeJson> Page=new ArrayList<TreeJson>();
		try{
			String sql="select du.* from disk_organization du where du.disabled = 0 and du.parentid = '" + pid +"' and du.type = '"+type+"'";
			Object[] args={};
			List<Map<String,Object>> lists=jdbcTemplate.findList(sql,args);
			List<TreeJson> rows=new ArrayList<TreeJson>();
			if(!CollectionUtils.isEmpty(lists)){
				lists.forEach(m->{
					TreeJson row=new TreeJson();
					row.setId(m.get("id")==null?"":m.get("id").toString());
					row.setLabel(m.get("label")==null?"":m.get("label").toString());
					row.setType(type);
					row.setDisabled(false);
					rows.add(row);
				});
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return Page;

	}

}
